package com.hengpeng.itfinoss.controller.projectinfo;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.web.tags.ListTag.Pagination;
import com.hengpeng.itfinbase.persist.ProjectInfo;
import com.hengpeng.itfinoss.controller.BaseController;
import com.hengpeng.itfinoss.controller.util.PageHandlerUtil;
import com.hengpeng.itfinoss.service.ServiceFactory;
/**
 * <pre>
 * 项目信息
 * </pre>
 * @author weiyuan
 * @version 1.0, 2015年8月24日
 */
@Controller
@RequestMapping("/project")
public class ProjectInfoController extends BaseController
{
	public final static Log logger = LogFactory.getLog(ProjectInfoController.class);

	@Autowired
	private ServiceFactory serviceFactory;
	
	/**
	 * <pre>
	 * 项目信息
	 * </pre>
	 * @param map
	 * @param request
	 * @param borrowName
	 * @param borrowUserName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list(ModelMap map, HttpServletRequest request)
	{
		Pagination page = new Pagination(request);
		page.setPageSize(this.getPageSize(request));
		ServiceResult<List<ProjectInfo>> service = serviceFactory.getProjectService().queryList(page.currentPage,
				page.pageSize);
		page.setEntityCount(service.getReturnListSize());
		map.put("projectlist", service.getReturnValue());
		map.put("page", page);
		return "project/projectlist";
	}

	/**
	 * <pre>
	 * 转向项目新增
	 * </pre>
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public String preCreate(ModelMap map)
	{
		ProjectInfo project = new ProjectInfo();
		map.put("project", project);
		return "project/addproject";
	}

	/**
	 * <pre>
	 * 新增项目
	 * </pre>
	 * @param request
	 * @param borrow
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(HttpServletRequest request, ProjectInfo project, ModelMap map)
	{
		try
		{
			project.setUpdateTime(new Date());
			project.setCreateTime(new Date());
			serviceFactory.getPersistService().save(project);
			return PageHandlerUtil.returnSuccess(map, "新增成功", "/project/list");
		}
		catch (Exception ex)
		{
			logger.error(ex);
			return PageHandlerUtil.returnError(map, ex);
		}
	}
	/**
	 * 获取项目
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/get/{id}")
	public String get(@PathVariable Long id, ModelMap map)
	{
		ProjectInfo obj = serviceFactory.getPersistService().get(ProjectInfo.class, id);
		map.put("project", obj);
		return "project/updateproject";
	}

	/**
	 * <pre>
	 * 修改项目信息
	 * </pre>
	 * @param request
	 * @param vo
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public String modify(HttpServletRequest request, ProjectInfo project, ModelMap map)
	{
		project.setUpdateTime(new Date());
		serviceFactory.getPersistService().update(project);
		return PageHandlerUtil.returnSuccess(map, "修改成功", "/project/list");
	}

}
